<?php namespace simpleFlow\view\html;

use simpleFlow\Object as SFObject;
use simpleFlow\view\View as SFView;
use simpleFlow\view\html as SFHtml;
use simpleFlow\request\RequestWeb as SFRequest;

/**
 * ListView html component
 * @package simpleFlow\view
 * @subpackage html
 * @author Guilherme Fontenele
 */
class ListView extends SFObject
{
	/**
	 * @var string ListView name
	 */
	public $strName;
	/**
	 * @var string Title
	 */
	public $strTitle;
	/**
	 * @var SFView View object
	 */
	public $objView;
	/**
	 * @var integer Selected page
	 */
	public $intSelectedPage;
	/**
	 * @var integer Total rows
	 */
	public $intTotal;
	/**
	 * @var integer Total pages
	 */
	public $intPages;
	/**
	 * @var array Header itens
	 */
	public $arrHeaders;
	/**
	 * @var array Rows
	 */
	public $arrContent;
	/**
	 * @var array Objects
	 */
	public $arrObjects;
	/**
	 * @var array Action Buttons
	 */
	public $arrActionButons;
	
	/**
	 * Constructor
	 * @param string $strName
	 * @param string $strTitle
	 * @param SFView $objView 
	 */
	public function __construct($strName, $strTitle = null, SFView $objView)
	{
		$this->strName	= $strName;
		$this->strTitle = $strTitle;
		$this->objView	= $objView;
		
		$this->configure();
	}
	
	/**
	 * Add a header
	 * @param integer $intIndex Index of header
	 * @param string $strLabel Label contents
	 * @param integer $intWidthPercents Width percents
	 * @param string $strAlign Align
	 */
	public function addHeader($intIndex,$strLabel,$intWidthPercents = null,$strAlign = 'Left')
	{
		$this->arrHeaders[$intIndex]['label'] = $strLabel;
		$this->arrHeaders[$intIndex]['align'] = $strAlign;
		$this->arrHeaders[$intIndex]['width'] = $intWidthPercents ? $intWidthPercents.'%' : null;
	}
	
	/**
	 * Add an action button collumn
	 * @param string $strType Type [open,]
	 * @param string $strLink Link
	 * @param string $strHintText Hint (example: )
	 * @param string $strCssJquery Icon in jQuery UI lib
	 * @todo Implements to define the index and the user can change the index / TODO:
	 */
	public function addActionButton($strType = 'open',$strLink = '#',$strHintText = '',$strCssJquery = 'ui-icon-folder-open')
	{
		$this->arrActionButons[$strType]['type']	= $strType;
		$this->arrActionButons[$strType]['link']	= $strLink;
		$this->arrActionButons[$strType]['hint']	= $strHintText;
		$this->arrActionButons[$strType]['css']		= $strCssJquery;
	}
	
	/**
	 * Define the ListView content
	 * @param array $arrContent Content of listview
	 * @param integer $intTotal Count of rows
	 */
	public function setContent($arrContent,$intTotal)
	{
		$this->arrContent	= $arrContent;
		$this->intTotal		= $intTotal;
		$this->intPages		= ceil($intTotal/SF_VIEW_LIST_ROWS);
	}
	
	/**
	 * Define the ListView content
	 * @param array $arrObjects Content of listview
	 * @param integer $intTotal Count of rows
	 */
	public function setObjects($arrObjects,$intTotal)
	{
		$this->arrObjects	= $arrObjects;
		$this->intTotal		= $intTotal;
		$this->intPages		= ceil($intTotal/SF_VIEW_LIST_ROWS);
	}
	
	/**
	 * Configure the component
	 */
	public function configure() {
		$this->intSelectedPage = SFRequest::getRequest()->hasParam('page') ? SFRequest::getRequest()->getParam('page') : 1;
	}
	
	/**
	 * Convert the object to string
	 * @return string Html content
	 */
	public function __toString()
	{
		$strTemplate = 'ListView.tpl';
		$_strHtml = "";
		$_strTemplateDir = $this->objView->getTemplateDir();
		$this->objView->setTemplateDir(SF_LIB_DIR_ROOT.'view/html/');
		$this->objView->setFetchMode(true);
		
		$this->objView->_lsName = $this->strName;
		
		$obj = new SFHtml\Button('','Go');
		$obj->addClass('btGoToPage');
		$this->objView->_btGoToPage = $obj;
		
		$obj = new SFHtml\InputText('',$this->intSelectedPage);
		$obj->addClass('txGoToPage');
		$obj->addAttribute('size',1);
		$this->objView->_txGoToPage = $obj;
		
		$this->objView->_strTitle			= $this->strTitle;
		$this->objView->_arrHeaders			= $this->arrHeaders;
		$this->objView->_arrContent			= $this->arrContent;
		$this->objView->_arrObjects			= $this->arrObjects;
		$this->objView->_arrActionButtons	= $this->arrActionButons;
		$this->objView->_intPages			= $this->intPages;
		$this->objView->_intTotal			= $this->intTotal;
		$this->objView->_intSelectedPage	= $this->intSelectedPage;
		$this->objView->_link				= SFRequest::getRequest()->removeParam('page')->getParsedURL().'/page/';
		
		$_strHtml.= $this->objView->show($strTemplate);
		
		$this->objView->setTemplateDir($_strTemplateDir[0]);
		$this->objView->setFetchMode(false);
		
		return $_strHtml;
	}
	
	/**
	 * Finish the render of ListView
	 */
	public function finish()
	{
		$this->objView->{$this->strName} = $this->__toString();
	}
}

?>